package com.hb.back.controller;

import com.alibaba.excel.EasyExcel;
import com.hb.back.domain.EduAclUser;
import com.hb.back.dto.LoginDto;

import com.hb.back.excel.DemoDataListener;
import com.hb.back.service.EduAclPermissionService;
import com.hb.back.service.impl.EduAclUserServiceImpl;
import com.hb.back.token.Token;
import com.hb.common.vo.MentVo;
import com.hb.common.domain.SubjectData;
import com.hb.common.service.EduSubjectService;
import com.hb.common.vo.ResultVo;
import  java.util.List;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    EduAclUserServiceImpl eduAclUserService;
    @Autowired
    PasswordEncoder passwordEncoder;
    @Autowired
    EduSubjectService subjectService;
    @Autowired
    EduAclPermissionService aclPermissionService;

    @PostMapping("/login")
    @Operation(summary = "登录接口")
    public ResultVo login(@RequestBody LoginDto loginDto){
        EduAclUser user=(EduAclUser)eduAclUserService.loadUserByUsername(loginDto.getUsername());
        //判断密码
        if(passwordEncoder.matches(loginDto.getPassword(),user.getPassword())){
        //生成密码
        String token = Token.getToken(user.getUsername());
        UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken=new UsernamePasswordAuthenticationToken(user.getUsername(),user.getPassword(),user.getAuthorities());
        SecurityContextHolder.getContext().setAuthentication(usernamePasswordAuthenticationToken);
          return ResultVo.success("登录成功!",token);
        }
        return ResultVo.error("账号密码错误!",null);
      }

    @GetMapping("/info")
    @Operation(summary = "info")
    public ResultVo userinfo(HttpServletRequest request){
        String token = request.getHeader("token");
        String name = Token.getName(token);
        List<MentVo> menus = aclPermissionService.getChilderMenus(name);
        Map map=new HashMap();
        map.put("name",name);
        map.put("avatar","https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        map.put("menus",menus);
        return ResultVo.success("",map);
     }

    @PostMapping("/excel")
    @Operation(summary = "excel导入")
    public ResultVo ti(MultipartFile file) throws IOException {
            //1 获取文件输入流
            InputStream inputStream = file.getInputStream();
            // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
            EasyExcel.read(inputStream, SubjectData.class, new DemoDataListener(subjectService)).sheet().doRead();
            return ResultVo.success("上传成功!",null);
    }

    @PostMapping("/logout")
    public ResultVo loginout(){
        return ResultVo.success("",null);
    }
}
